/**
* All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
*
* PmReceiptApplyServiceAuto
* 项目收据申请
*
* 修改纪录
* 2020-05-21 版本：1.0 wushenghong 创建。
* @version 版本：1.0
* @author 作者：wushenghong
* 创建日期：2020-05-21
*/

package com.topscomm.pm.service.impl.auto;

import com.topscomm.pm.service.impl.PmProjectService;
import javax.annotation.Resource;
import com.topscomm.basic.IBasicDao;
import com.topscomm.tap.common.DatacodeCache;
import java.util.Map;
import com.topscomm.pm.*;
import com.topscomm.pm.pojo.PmReceiptApplyEntity;
import com.topscomm.pm.dao.IPmReceiptApplyDao;
import lombok.SneakyThrows;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;


public class PmReceiptApplyServiceAuto extends PmService<PmReceiptApplyEntity,IPmReceiptApplyDao> {

	@Autowired
	private PmProjectService pmProjectService;

	/**
	 * @author: wushenghong
	 * @date: 2020-05-21
	 * @param mapRow
	 * @description:设置字段显示值
	 * @modify:
	 */
	@SneakyThrows
	@Override
	public void setDispFields(Map<String, Object> mapRow, DatacodeCache datacodeCache) {
		String temp;
		if (mapRow.get("projectid") != null) {
			temp = mapRow.get("projectid").toString();
			mapRow.put("dispprojectid", datacodeCache.getValueDynamic("PmProject", temp, "id", "name"));
		}
		if (mapRow.get("receivertype") != null) {
			temp = mapRow.get("receivertype").toString();
			mapRow.put("dispreceivertype", datacodeCache.getValueDynamic("PmReceiptReceiverType", temp, "code", "name"));
		}
		if (mapRow.get("category") != null) {
			temp = mapRow.get("category").toString();
			mapRow.put("dispcategory", datacodeCache.getValueDynamic("PmInvoiceApplyCategory", temp, "code", "name"));
		}
		if (mapRow.get("courier") != null) {
			temp = mapRow.get("courier").toString();
			mapRow.put("dispcourier", datacodeCache.getValueDynamic("PmCourier", temp, "code", "name"));
		}
		if (mapRow.get("receiptdatetype") != null) {
			temp = mapRow.get("receiptdatetype").toString();
			mapRow.put("dispreceiptdatetype", datacodeCache.getValueDynamic("PmReceiptReceiptDatetype", temp, "code", "name"));
		}
		if (mapRow.get("isreceiptreturn") != null) {
			temp = mapRow.get("isreceiptreturn").toString();
			mapRow.put("dispisreceiptreturn", datacodeCache.getValueDynamic("CboYesNo", temp, "code", "name"));
		}
		if (mapRow.get("flowid") != null) {
			temp = mapRow.get("flowid").toString();
			mapRow.put("dispflowid", datacodeCache.getValueDynamic("ApprovalFlow_PmReceiptApply", temp, "id", "name"));
		}
		if (mapRow.get("state") != null) {
			temp = mapRow.get("state").toString();
			mapRow.put("dispstate", datacodeCache.getValueDynamic("TapApprovalBillState", temp, "code", "name"));
		}
		if (mapRow.get("deptname") != null && mapRow.get("province")!=null) {
			// 供应链-崔倩需求，部门名称只展示二级部门，如果部门名称包含“营销服务中心”字样，则替换为空
			temp = mapRow.get("deptname").toString();
			String[] splitDept = temp.split("\\\\");
			if (splitDept.length>=2&&splitDept[1].contains(PmSystemConst.DeptNameReplaceStrEnum.ReplaceStr)){
				mapRow.put("provinceTop", splitDept[1].replace(PmSystemConst.DeptNameReplaceStrEnum.ReplaceStr,""));
			}else {
				// 使用默认省份
				mapRow.put("provinceTop",  mapRow.get("province").toString());
			}
		}

		// 收据开具查询的列表展示"所属区域"字段
		// 逻辑：1>先根据部门编码判断是否为一级部门，如果是一级部门的，直接展示“省区公共”
		//      2>不是一级部门的话，从CRM系统获取对应的区域信息。如果返回来有值，则对部门名称做处理，截取最后一段的字符串再展示，如果返回来的没有值，直接展示“省区公共“
		if (mapRow.get("deptname") != null && mapRow.get("deptcode")!=null) {
			String deptnametemp = mapRow.get("deptname").toString();
			String deptcodetemp= mapRow.get("deptcode").toString();
			// 判断是否为一级部门
			if((deptcodetemp.contains("_") && deptcodetemp.substring(deptcodetemp.indexOf('_')+1).length()==4)||deptcodetemp.length()==4){
				mapRow.put("dispownerarea","省区公共");
			}else{
				String querycode="";
				if(deptcodetemp.contains("_")){
					querycode=deptcodetemp.substring(0,deptcodetemp.indexOf('_')+7);
				}else{
					querycode=deptcodetemp.length()>=6?deptcodetemp.substring(0,6):deptcodetemp;
				}
				try {
					String areaInfoFromCrmForquery = pmProjectService
						.getAreaInfoFromCrmForquery(querycode);
					if(!Strings.isEmpty(areaInfoFromCrmForquery)){
						// 1>因为CRM中也配置了一级部门的区域编码(CRM系统的需要)，所以需要判断是否包含"区域"二字
						// 2>有的区域部门没有"区域"二字，比如新疆的一些区域部门,所以还需要根据部门code做判断
						if(deptcodetemp.contains("区域")||(deptcodetemp.contains("_") && deptcodetemp.substring(deptcodetemp.indexOf('_')+1).length()==6)||deptcodetemp.length()==6){
							String[] split = deptnametemp.split("\\\\");
							mapRow.put("dispownerarea",split[split.length-1]);
						} else{
							mapRow.put("dispownerarea","省区公共");
						}
					}else{
						mapRow.put("dispownerarea","省区公共");
					}
				} catch (Exception e) {
					throw new Exception("外部系统配置区域编码有误，请联系本系统管理员！");
				}
			}
		}
		if (mapRow.get("enabled") != null) {
			temp = mapRow.get("enabled").toString();
			mapRow.put("dispenabled", datacodeCache.getValueDynamic("CboYesNo", temp, "code", "name"));
		}
	}

	@Override
	@Resource(name = "pmReceiptApplyDao")
	public void setDao(IBasicDao<PmReceiptApplyEntity> dao) {
		super.setDao(dao);
	}

	public static void main(String[] args) {
		String aa = "aaa\\\\bb\\\\cc";
		System.out.println(aa.split("\\\\"));
	}
}
